package algorithmTopics.array;

import java.util.Arrays;
import java.util.HashMap;

public class test9 {
    public static void main(String[] args) {

        test9 test7 = new test9();
        int[] ints = new int[]{0,0,1,9};
        int[] ints1 = test7.twoSum(ints, 10);
        System.out.println(Arrays.toString(ints1));
    }

    public int[] twoSum(int[] nums, int target){
        HashMap<Integer, Integer> map = new HashMap<>(nums.length - 1);
        for (int i = 0; i < nums.length; i++) {
            if (map.get(target - nums[i]) != null) {
                return new int[]{i, map.get(target - nums[i])};
            }
            map.put(nums[i],i);
        }

        return new int[]{};
    }

    /*public int[] twoSum(int[] nums, int target) {
        //暴力破解暴力破解法，慢
        int[] ans = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length ; j++) {
                if (nums[i] + nums[j] == target) {
                    ans[0] = i;
                    ans[1] = j;
                    break;
                }
            }
        }

        return ans;
    }*/


}
